package com.spring.component.test.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ClassName: SocketClient
 * @Description:
 * @author: liushihua
 * @date: 2018/11/29
 **/
public class SocketClient {
	public static final String IP = "127.0.0.1";

	public static final int PORT = 5000;

	public static void main(String[] args) throws Exception {
		Socket client = new Socket("10.180.221.105", 20001);
		OutputStream os = client.getOutputStream();
		client.sendUrgentData(0xFF);
		Thread.sleep(3000);
		os.write("abc".getBytes("GBK"));
		os.flush();
		System.out.println("send abc");
		Thread.sleep(3000);
		os.write("123".getBytes("GBK"));
		os.flush();
		System.out.println("send 123");
	}

	public static void test() {
		Scanner scanner = new Scanner(System.in);
		try {
			Socket client = new Socket(IP, PORT);
			System.out.println("client connect server ");
			// client.sendUrgentData(0xFF);
			InputStream is = client.getInputStream();
			OutputStream os = client.getOutputStream();
			// new Thread(() -> {
			// 	System.out.println("========开启线程接收返回结果=========");
			// 	byte[] buffer = new byte[1024];
			// 	while (true) {
			// 		if (client.isConnected()) {
			// 			try {
			// 				int size = is.read(buffer);
			// 				if (size <= 0) {
			// 					System.err.println("Socket reciver异常！返回size " + size);
			// 				} else {
			// 					System.out.println("接收返回结果：" + new String(buffer, "UTF-8"));
			// 				}
			// 			} catch (IOException e) {
			// 				e.printStackTrace();
			// 				break;
			// 			}
			// 		}
			// 	}
			// }).start();
			while (client.isConnected()) {
				System.out.println("请输入要发送的信息：");
				String message = scanner.nextLine();
				System.out.println("send message: " + message);
				os.write(message.getBytes("UTF-8"));
				os.flush();
				// try {
				// 	Thread.sleep(3000);
				// } catch (InterruptedException e) {
				// 	e.printStackTrace();
				// }
			}
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void connect() {

	}
}
